from flask import Flask,render_template
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from config import config

bootstrap = Bootstrap()
db = SQLAlchemy()
login_manager = LoginManager()
login_manager.session_protection = 'strong' # 高安全等级，记录用户登录的 ip和 UA，发现异常就登出用户
login_manager.login_view = 'auth.login'

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    bootstrap.init_app(app)
    db.init_app(app)
    login_manager.init_app(app)

    # 注册蓝本
    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint,url_prefix='/auth')

    # 不再使用 0.2 版
    # from .api_0_2 import api_0_2 as api_0_2_blueprint
    # app.register_blueprint(api_0_2_blueprint)
    # 不再使用 0.3 版
    # from .api_0_3 import api_0_3 as api_0_3_blueprint
    # app.register_blueprint(api_0_3_blueprint,url_prefix='/api/0.3')

    from .api_0_4 import api_0_4 as api_0_4_blueprint
    app.register_blueprint(api_0_4_blueprint,url_prefix='/api/0.4')

    from .api_6 import api_6 as api_6_blueprint
    app.register_blueprint(api_6_blueprint,url_prefix='/api/6')

    return app
